
/**
 * 调用接口获取公司信息
 */
function getNationData(data) {
  let { taxType } = data;
  let institutionCode = window.baseInfoCache.companyInfo.institutionCode === "1" ? '2007' : "2013"
  let taxInfo = {
    institution: institutionCode, // 国税对应制度 2007 | 2013
    nsrsbh: 'test', // 纳税人申报号
    nsrmc: 'test', // 纳税人名称（纳税公司）
    skssqq: '', // 税款所属期起
    skssqz: '', // 税款所属期止
    isZip: false,
    isHsqjSmall: false, // 是否为小微企业汇算清缴
    saasInstitution: '' // 税局对应的企业会计制度
  };
  if (taxType == 'sds' || taxType == 'zzs') {
    window.postMessage(
      {
        event: 'backNationData',
        data: {
          taxInfo: taxInfo
        }
      },
      '*'
    );
    return;
  }
  if (taxType === 'hsqj') {
    taxInfo.isHsqjSmall = institutionCode === "2013";
    window.postMessage(
      {
        event: 'backNationData',
        data: {
          taxInfo: taxInfo
        }
      },
      '*'
    );
    return true;
  }
  let location = window.location;
  let port = location.port;
  let type = 'WLSB';
  if (port == '37013') {
    type = 'dswlsb';
  }
  // 获取财务报表数据
  if (['balance', 'profit', 'cash'].includes(taxType)) {
    try {
      let contentIframe = document.querySelector('#allframe > div > div.x-tab-panel-body > iframe').contentDocument;
      let input = contentIframe.querySelector("#zzsybnsrsb > div.tb_mini_box.toolbar_fixed > div.c > div.tb_blank > div > table > tbody > tr > td:nth-child(2) > div > table > tbody > tr > td.boxtd > input");
      let inputTest = input.value || input.getAttribute('originvalue') || '';
      if (inputTest.includes('未执行新金融准则')) {
        taxInfo.saasInstitution = 1;
      }
      if (taxType === cash && inputTest.includes('执行企业会计准则')) {
        taxInfo.saasInstitution = 1;
      }
      if (inputTest.includes('执行小企业会计准则')) {
        taxInfo.saasInstitution = 2;
      }  
    } catch (e) {
      console.log(e)
    }
  }

  window.postMessage(
    {
      event: 'backNationData',
      data: {
        taxInfo: taxInfo
      }
    },
    '*'
  );
}
// 增值税 和 所得税
async function fillInput({ saasData }) {
  let sheet = saasData.sheet;
  if (!$iframe) {
    let iframeD = document.querySelector('#ym-ml .ym-body iframe');
    $iframe = iframeD.contentDocument;
    if (!$iframe) {
      window.location.onload();
    } else {
      changeType();
    }
  }
  let tableType;
  let checkType = '《资产负债表（适用执行小企业会计准则的企业）》';
  const typeSign = $iframe.querySelector('input[name=SwordTreeJSON]');
  if (!typeSign) {
    checkType = typeSign.getAttribute('originvalue')
      || '《资产负债表（适用执行小企业会计准则的企业）》';
  }
  if (!namesKeys.includes(names[sheet].name)) {
    alert('填报报表与一键报税填报报表类型不匹配请核实');
    return;
  }
  let currentIframe;
  if (typeSign.getAttribute('realvalue')) {
    currentIframe = $iframe.getElementById(
      `table${typeSign.getAttribute('realvalue')}iframe`
    ).contentDocument;
  } else {
    currentIframe = $iframe.getElementById('table00iframe').contentDocument;
  }
  tableType = names[sheet].code;
  let message = '填报成功';
  try {
    let inputData = saasData.data;
    if (sheet == 'sds' || sheet == 'zzs') {
      // inputData = delSdsData(inputData.openParams.inputData.data.profit);
    }
    for (let key in inputData) {
      const input = currentIframe.getElementById(`${tableType}${key}`);
      input
        .set('value', inputData[key])
        .set('realvalue', inputData[key])
        .fireEvent('change');
    }
  } catch (e) {
    message = '填报失败' + e;
  }

  // todo finally
  window.postMessage(
    {
      event: 'fillInputDone',
      data: {
        message: message || '填报成功'
      }
    },
    '*'
  );
}


export { getNationData, fillInput };